﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using ImageEffects;
using RootMotion.FinalIK;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;
using Random = System.Random;

namespace Gameplay.PVE.Survivor
{
    public class MeshBaker:IClass
    {
        private struct GhostShadow
        {
            public GameObject gameObject;
            public MeshApplyData meshData;
            public float fadeTime;
            public float endTime;
        }
        
        private List<GhostShadow> shadowList = new List<GhostShadow>();
        private SkinnedMeshRenderer[] meshRender;
        public void Initialize(GameObject gameObject)
        {
            meshRender = gameObject.GetComponentsInChildren<SkinnedMeshRenderer>();
        }

        public void Bake(float time = 0f)
        {
            for (int i = 0; i < meshRender.Length; i++)
            {
                if (meshRender[i] == null)
                {
                    return;
                }
                Mesh mesh = new Mesh ();
                meshRender[i].BakeMesh(mesh);

                GameObject go = new GameObject();
                //go.hideFlags = HideFlags.HideAndDontSave;//面板不显示

                MeshFilter filter = go.AddComponent<MeshFilter>();
                filter.mesh = mesh;

                MeshRenderer meshRen = go.AddComponent<MeshRenderer>();

                var meshData = ClassManager.Get<MeshApplyData>();
                meshData.renderer = meshRen;
                BattleResourceManager.Instance.ApplyPhantomMaterial(meshData);

                go.transform.localScale = meshRender[i].transform.localScale;
                go.transform.position = meshRender[i].transform.position;
                go.transform.rotation = meshRender[i].transform.rotation;

                
                //item.meshRenderer = meshRen;
                shadowList.Add(new GhostShadow
                {
                    gameObject = go,
                    meshData = meshData,
                    endTime = TimeManager.gameTime + time,
                    fadeTime = 1.2f,
                });
            }
        }

        public void Update()
        {
            CheckShadowLifeTime();
        }
        
        
        private void CheckShadowLifeTime()
        {
            for (int i = 0; i < shadowList.Count; i++)
            {
                if (TimeManager.gameTime > shadowList[i].endTime + shadowList[i].fadeTime)
                {
                    GameObject.DestroyImmediate(shadowList[i].gameObject);
                    ClassManager.Free(shadowList[i].meshData);
                    shadowList.RemoveAt(i);
                    i--;
                }
                else if (TimeManager.gameTime > shadowList[i].endTime)
                {
                    float percent = (TimeManager.gameTime - shadowList[i].endTime) / shadowList[i].fadeTime;
                    var meshData = shadowList[i].meshData;
                    if (meshData.materialReady)
                    {
                        var color = meshData.material.GetColor("_Color");
                        float alpha = 1 - percent;
                        percent = 0;
                        var newColor = new Color(255f/255f,150/255f,255/255f,alpha);
                        meshData.material.SetColor("_Color",newColor);
                    }
                }
            }
        }

        public void ClearShadow()
        {
            for (int i = 0; i < shadowList.Count; i++)
            {
                GameObject.DestroyImmediate(shadowList[i].gameObject);
            }
            shadowList.Clear();
        }

        public void OnReset()
        {
            
        }
    }
}
